package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.Basket;
import cn.com.dcsgo.dto.CartInfoDTO;
import cn.com.dcsgo.service.BasketService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CartRPCController
 *
 * @author Dcsgo
 * @since 2025/11/17 14:07
 */
@Tag(name = "购物车RPC相关接口")
@RestController
@RequestMapping("rpc/cart")
public class CartRPCController {
    @Resource
    private BasketService basketService;

    @GetMapping("list/ids")
    @Operation(summary = "根据ID列表获取购物车信息")
    public List<CartInfoDTO> getBasketList(@RequestParam("basketId") List<Long> basketIds) {
        return basketService.listByIds(basketIds).stream().map(basket -> {
            CartInfoDTO cartInfoDTO = new CartInfoDTO();
            BeanUtil.copyProperties(basket, cartInfoDTO);
            return cartInfoDTO;
        }).toList();
    }

    @DeleteMapping("openid/{openid}")
    @Operation(summary = "批量删除购物车数据")
    public boolean removeCartItem(@PathVariable("openid") String openid, @RequestParam("skuId") List<Long> skuIds) {
        return basketService.remove(
                new LambdaQueryWrapper<Basket>()
                        .eq(Basket::getOpenId, openid)
                        .in(Basket::getSkuId, skuIds)
        );
    }
}
